package net.edu_soft.ec.asset.service.impl;

import net.edu_soft.ec.asset.mapper.CloudMessageMapper;
import net.edu_soft.ec.asset.mapper.MessageTypeMapper;
import net.edu_soft.ec.asset.pojo.CloudMessage;
import net.edu_soft.ec.asset.pojo.CloudMessageExample;
import net.edu_soft.ec.asset.service.CloudMessageService;
import net.edu_soft.ec.common.exception.ParameterException;
import net.edu_soft.ec.common.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CloudMessageServiceImpl implements CloudMessageService {
    @Autowired
    private MessageTypeMapper messageTypeMapper;

    @Autowired
    private CloudMessageMapper cloudMessageMapper;

    @Value("${serviceIp}")
    private String imageip;

    @Value("${imagePath}")
    private String imagePath;

    @Override
    public int addCloudMessage(CloudMessage cloudMessage, MultipartFile mainFile) {
        String filesurl=null;
        String filename=null;
        //检验数据
        if (cloudMessage.getTitle()!=null && cloudMessage.getSummary()!=null &&
                cloudMessage.getAuthor()!=null && cloudMessage.getContent()!=null){
            //验证message_type类型是否存在
            if (messageTypeMapper.selectByPrimaryKey(Integer.valueOf(cloudMessage.getMessageType()))!=null){
                if (mainFile!=null){
                    //存入图片
                    filesurl="gjy/message";//定义消息图片存储路径：/home/image/Advertising
                    String uuid= UUID.randomUUID().toString();
                    filename="message_"+uuid+".jpg";//定义图片的文件名称
                }
                cloudMessage.setImages(imageip+filesurl+"/"+filename);
                cloudMessage.setStatus(1);
                cloudMessage.setCreateDate(new Date());
                //存入数据库
                if(cloudMessageMapper.insert(cloudMessage)==1){
                    //存入图片
                    int iscun= FileUtils.getImgUrl(mainFile,imagePath+filesurl,filename);
                    if (iscun!=0){
                        return 1;
                    }
                }
            }else {
                new ParameterException("不存在此消息类型");
            }
        }
        return 0;
    }

    @Override
    public List<CloudMessage> getCloudMessageList() {
        CloudMessageExample cloudMessageExample=new CloudMessageExample();
        return  cloudMessageMapper.selectByExample(cloudMessageExample);
    }

    @Override
    public int updataCloudMessage(CloudMessage cloudMessage, MultipartFile mainFile) {
        String filesurl="gjy/message";//定义图片存储路径
        String uuid= UUID.randomUUID().toString();
        String filename="message_"+uuid+".jpg";//定义图片的文件名称
        String yurl[]=cloudMessageMapper.selectByPrimaryKey(cloudMessage.getCloudMessageId()).getImages().split("gjy");
        if (mainFile!=null){
            FileUtils.getImgUrl(mainFile,imagePath+filesurl,filename);//添加图片
            cloudMessage.setImages(imageip+filesurl+"/"+filename);
            //删除源图片
            FileUtils.delete(imagePath+"gjy"+yurl[1]);
        }
        if (cloudMessageMapper.selectByPrimaryKey(cloudMessage.getCloudMessageId())!=null){
            CloudMessageExample cloudMessageExample=new CloudMessageExample();
            CloudMessageExample.Criteria criteria=cloudMessageExample.createCriteria();
            criteria.andCloudMessageIdEqualTo(cloudMessage.getCloudMessageId());
            if(cloudMessageMapper.updateByExampleSelective(cloudMessage,cloudMessageExample)==1){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int deleCloudMessage(Integer id) {
        return cloudMessageMapper.deleteByPrimaryKey(id);
    }


}
